package com.lovi.map_;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用HashMap添加3个员工对象，要求
 * 键：员工id
 * 值：员工对象
 * 并遍历显示工资>18000的员工（遍历方式最少两种)
 * 员工类：姓名、工资、员工id
 */
public class Map01 {
    public static void main(String[] args) {
        Map map = new HashMap();
        Employee e1 = new Employee("lovi", 10000, 1);
        Employee e2 = new Employee("lovi2", 60000, 2);
        Employee e3 = new Employee("lovi3", 70000, 3);
        //添加
        map.put(e1.getId(), e1);
        map.put(e2.getId(), e2);
        map.put(e3.getId(), e3);
        //并遍历显示工资>18000的员工（遍历方式最少两种)
        //通过keyset遍历
        System.out.println("通过keyset遍历");
        for (Object key : map.keySet()) {
            Employee e = (Employee)map.get(key);
            if(e.getSalary()>18000){
                System.out.println(e);
            }
        }
        System.out.println("通过entryset遍历");
        for (Object entry : map.entrySet()) {
            Employee e = (Employee)((Map.Entry)entry).getValue();
            if(e.getSalary()>18000){
                System.out.println(e);
            }
        }


    }
}
class Employee{
    private String name;
    private double salary;
    private int id;
    public Employee(String name, double salary, int id) {
        this.name = name;
        this.salary = salary;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", id=" + id +
                '}';
    }
}
